package com.leanyuan.check.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.check.domain.Conditiontask;
import com.leanyuan.check.domain.Positionplan;
import com.leanyuan.check.dto.Conditiontaskdto;
import com.leanyuan.check.service.ConditiontaskService_C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@CrossOrigin
@RestController
@RequestMapping("/leanyuan/Conditiontask")
public class ConditiontaskController {

    @Autowired
    ConditiontaskService_C conditiontaskServiceC;

    @RequestMapping("FindAllConditiontask")
    public Page<Conditiontaskdto> FindAllPositionplan(int pagesize, int pageno, Model model){
        System.out.println("巡检计划");
        Page<Conditiontaskdto> list= conditiontaskServiceC.ConditiontasklistPage(pageno,pagesize);
        model.addAttribute("Positionplanlist",list);
        return list;
    }
    //分页查询
    @RequestMapping("FindAllConditiontask1")
    public Page<Conditiontaskdto> FindAllPositionplan1(int pagesize, int pageno, String id, String name, String taskcondition, Model model) throws ParseException {
        Conditiontaskdto conditiontaskdto=new Conditiontaskdto();
        if(id!=null&&id!="")conditiontaskdto.setConditiontaskId(Integer.parseInt(id));
        else conditiontaskdto.setConditiontaskId(null);
        conditiontaskdto.setPositionplan(new Positionplan().setName(name));
        conditiontaskdto.setTaskcondition(taskcondition);
        System.out.println(conditiontaskdto);
        Page<Conditiontaskdto> list= conditiontaskServiceC.ConditiontasklistPage1(pageno,pagesize,conditiontaskdto);
        model.addAttribute("Positionplanlist",list);
        return list;
    }

    //添加任务
    @RequestMapping("SaveConditiontask")
    public String SaveConditiontask(String positionplanId,String staffId,String conditiontime,String methods){
        Conditiontask conditiontask=new Conditiontask();
        if(positionplanId!=null&&positionplanId!="")conditiontask.setPositionplanId(Integer.parseInt(positionplanId));
        if(staffId!=null&&staffId!="")conditiontask.setStaffId(Integer.parseInt(staffId));
        conditiontask.setConditiontime(conditiontime);
        conditiontask.setTime(conditiontime);
        conditiontask.setTaskcondition("未完成");
        conditiontask.setMethods(methods);
        System.out.println(conditiontask);
        boolean flag= conditiontaskServiceC.save(conditiontask);
        if(flag)return "添加成功";
        return "添加失败";
    }
    //修改任务
    @RequestMapping("UpdateConditiontask")
    public String UpdateConditiontask(@RequestBody Conditiontask conditiontask){
        System.out.println(conditiontask);
        QueryWrapper<Conditiontask> queryWrapper=new QueryWrapper<Conditiontask>();
        queryWrapper.eq("conditiontask_id",conditiontask.getConditiontaskId());
        boolean flag= conditiontaskServiceC.update(conditiontask,queryWrapper);
        if(flag)return "修改成功";
        return "修改失败";
    }
    //删除任务
    @RequestMapping("DeleteConditiontask")
    public String DeleteConditiontask(Integer id){
        boolean flag= conditiontaskServiceC.removeById(id);
        if(flag)return "删除成功";
        return "删除失败";
    }
}
